/**
 * $RCSfile$
 * $Revision: 1705 $
 * $Date: 2005-07-26 14:10:33 -0300 (Tue, 26 Jul 2005) $
 *
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jawa.user.event;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.jawa.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 调度用户事件。每个事件都有一个类型 {@link EventType type} 。
 * <p>
 * 
 * <table border="1">
 * <tr>
 * <th>Event Type</th>
 * <th>Extra Params</th>
 * </tr>
 * <tr>
 * <td>{@link EventType#user_created user_created}</td>
 * <td><i>None</i></td>
 * </tr>
 * <tr>
 * <td>{@link EventType#user_deleting user_deleting}</td>
 * <td><i>None</i></td>
 * </tr>
 * <tr valign="top">
 * <td>{@link EventType#user_modified user_modified}</td>
 * <td>
 * <i>None</i>
 * </td>
 * </tr>
 * </table>
 * 
 */
public class UserEventDispatcher {

	private static final Logger Log = LoggerFactory
			.getLogger(UserEventDispatcher.class);

	private static List<UserEventListener> listeners = new CopyOnWriteArrayList<UserEventListener>();

	private UserEventDispatcher() {
		// 无法实例化.
	}

	/**
	 * 注册一个监听器去处理接收到的事件.
	 * 
	 * @param listener
	 *            监听器.
	 */
	public static void addListener(UserEventListener listener) {
		if (listener == null) {
			throw new NullPointerException();
		}
		listeners.add(listener);
	}

	/**
	 * 注销指定的监听器.
	 * 
	 * @param listener
	 *            监听器.
	 */
	public static void removeListener(UserEventListener listener) {
		listeners.remove(listener);
	}

	/**
	 * 调度所有的监听器.
	 * 
	 * @param user
	 *            用户.
	 * @param eventType
	 *            事件类型.
	 * @param params
	 *            事件参数.
	 */
	public static void dispatchEvent(User user, EventType eventType,
			Map<String, Object> params) {
		for (UserEventListener listener : listeners) {
			try {
				switch (eventType) {
				case user_created: {
					listener.userCreated(user, params);
					break;
				}
				case user_deleting: {
					listener.userDeleting(user, params);
					break;
				}
				case user_modified: {
					listener.userModified(user, params);
					break;
				}
				default:
					break;
				}
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
	}

	/**
	 * 事件类型.
	 */
	public enum EventType {

		/**
		 * 当用户被创建.
		 */
		user_created,

		/**
		 * 当用户被删除.
		 */
		user_deleting,

		/**
		 * 当用户属性被修改.
		 */
		user_modified
		
	}
}